package com.mall.common.domain.content;

import java.text.MessageFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.mall.common.domain.BaseEntity;
import com.mall.common.util.CommonSettingUtils;

/**
 * 文章实体
 */
@Entity
@Table(name = "s_article")
@JsonIgnoreProperties(ignoreUnknown=true) 
public class ArticleDomain extends BaseEntity {

	private static final long serialVersionUID = 3146346094290590195L;
	private String title;// 标题
	private String titleImg;// 标题图片
	private Boolean hasTitleImg;// 是否有标题图片
	private String author;// 作者
	private String content;// 用于文章接受内容
	private String contentImg;
	private String source;// 来源
	private String outerUrl; // 外部链接
	private String metaKeywords;// 页面关键词
	private String metaDescription;// 页面描述
	private String related_ids; // 相关文章
	private Boolean isPublication;// 是否发布
	private Boolean isTop;// 固顶级别
	@DateTimeFormat(pattern="yyyy-MM-dd")private Date releaseDate; // 发布时间
	private Boolean isDisabled; // 是否禁用
	private Boolean isRecommend;// 是否为推荐文章
	private Boolean allowComment;// 是否允许评论
	private Integer views; //点击次数
	@JsonIgnore private ArticleCategoryDomain category;// 文章所属分类

	@PrePersist
	public void prePersist() {
		if (this.isPublication == null) {
			this.setIsPublication(false);
		}
		if (this.isRecommend == null) {
			this.setIsRecommend(false);
		}
		if (this.isTop == null) {
			this.setIsTop(false);
		}
	}
	
	/**
	 * 获取相对路径
	 * @return
	 */
	@Transient
	public String getRelatePath(){
		if(category == null 
				|| StringUtils.isBlank(category.getTemplate())){
			return null;
		}
		return MessageFormat.format(category.getTemplate(), getId().toString());
	}

	/**
	 * 静态访问路径
	 * @return
	 */
	@Transient
	public String getPath() {
		if(StringUtils.isNotBlank(outerUrl)){
			return outerUrl;
		}
		if(category == null 
				|| StringUtils.isBlank(category.getTemplate())){
			return null;
		}
		StringBuilder url = CommonSettingUtils.get().getUrlBuff(true).append(MessageFormat.format(category.getTemplate(), getId().toString()));
		return url.toString();
	}

	/**
	 * 获取关于我们文章路径
	 * @return
	 */
	@Transient
	public String getAboutPath(){
		return MessageFormat.format(category.getTemplate(), getId().toString());
	}

	@Column(nullable = false)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	@Column(length = 10000, nullable = false)
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	public String getContentImg() {
		return contentImg;
	}

	public void setContentImg(String contentImg) {
		this.contentImg = contentImg;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getOuterUrl() {
		return outerUrl;
	}

	public void setOuterUrl(String outerUrl) {
		this.outerUrl = outerUrl;
	}

	public String getMetaKeywords() {
		return metaKeywords;
	}

	public void setMetaKeywords(String metaKeywords) {
		this.metaKeywords = metaKeywords;
	}

	public String getMetaDescription() {
		return metaDescription;
	}

	public void setMetaDescription(String metaDescription) {
		this.metaDescription = metaDescription;
	}

	public String getRelated_ids() {
		return related_ids;
	}

	public void setRelated_ids(String related_ids) {
		this.related_ids = related_ids;
	}

	@Column(columnDefinition = "int(11) default 0")
	public Boolean getIsPublication() {
		return isPublication;
	}

	public void setIsPublication(Boolean isPublication) {
		this.isPublication = isPublication;
	}

	@Column(columnDefinition = "int(11) default 0")
	public Boolean getIsTop() {
		return isTop;
	}

	public void setIsTop(Boolean isTop) {
		this.isTop = isTop;
	}

	public Date getReleaseDate() {
		return releaseDate;
	}

	public void setReleaseDate(Date releaseDate) {
		this.releaseDate = releaseDate;
	}

	@Column(columnDefinition = "int(11) default 0")
	public Boolean getIsDisabled() {
		return isDisabled;
	}

	public void setIsDisabled(Boolean isDisabled) {
		this.isDisabled = isDisabled;
	}

	public Boolean getIsRecommend() {
		return isRecommend;
	}

	public void setIsRecommend(Boolean isRecommend) {
		this.isRecommend = isRecommend;
	}

	public Boolean getAllowComment() {
		return allowComment;
	}

	public void setAllowComment(Boolean allowComment) {
		this.allowComment = allowComment;
	}

	public void setCategory(ArticleCategoryDomain category) {
		this.category = category;
	}

	public String getTitleImg() {
		return titleImg;
	}

	public void setTitleImg(String titleImg) {
		this.titleImg = titleImg;
	}

	public Boolean getHasTitleImg() {
		return hasTitleImg;
	}

	public void setHasTitleImg(Boolean hasTitleImg) {
		this.hasTitleImg = hasTitleImg;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "category_id")
	public ArticleCategoryDomain getCategory() {
		return category;
	}

	public Integer getViews() {
		return views;
	}

	public void setViews(Integer views) {
		this.views = views;
	}

}
